<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2017/3/28
 * Time: 10:30
 */
namespace app\home\controller;
class Base extends \think\Controller
{
    protected $site;
    private $controller;
    private $seo;
    protected $userInfo;
    protected $cityInfo;
    protected $menuSeo = true;
    public function _initialize(){
        parent::_initialize();
        if(request()->isMobile()){
            $this->redirect(url('Index/index@m').request()->url());
        }
        $domain = request()->host();
        $second = str_replace('.'.config('url_domain_root'),'',$domain);
        $this->cityInfo = cookie('cityInfo');
        if((!$this->cityInfo || $this->cityInfo['domain']!=$second) && $second!='www'){
            $this->getDomainByCity($second);
        }
        $this->checkUserLogin();
        $ip = new \org\Badip();
        $ip->checkIp();
        $site = cache('site');
        if($site['status'] == 0){
            die($site['reson']);
        }
        $this->site = $site;
        $this->seo = [
            'title'     => $site['title'],
            'seo_title' => $site['seo_title'],
            'seo_keys'  => $site['seo_keys'],
            'seo_desc'  => $site['seo_desc']
        ];
        $qq = str_replace('，',',',$site['qq']);
        $qq = explode(',',$qq);
        $this->controller = strtolower(request()->controller());
        $this->getMenu();
        $this->setSeo();
        $this->getCate();//单页分类
        $this->getCity();
        $this->assign('site',$site);
        $this->assign('qq',$qq);
        $this->assign('controller',$this->controller);
        $this->assign('cityInfo',$this->cityInfo);
    }
    //设置站点优化
    protected function setSeo($info='',$field='title',$q=''){
        if(!empty($info)){
            $info['title'] = isset($info['title']) ? $info['title'] : $info[$field];
            $seo['title'] = empty($info['seo_title']) ? $info[$field].'_'.$this->seo['title'] : $info['seo_title'].'_'.$this->seo['title'];
            $seo['keys']  = empty($info['seo_keys']) ? (empty($info['seo_title']) ? $info['title']:$info['seo_title']) : $info['seo_keys'];//$seoarr[$key]['keys'];
            $seo['desc']  = empty($info['seo_desc']) ? '' : $info['seo_desc'];//$seoarr[$key]['desc'];
        }else{
            $site['title'] = empty($q) ? $this->seo['seo_title'].'_'.$this->seo['title'] : $q .'_'.$this->seo['title'];
            $site['keys']  = empty($this->seo['seo_keys']) ? $this->seo['seo_title'] : $this->seo['seo_keys'];
            $site['desc']  = empty($this->seo['seo_desc']) ? '' : $this->seo['seo_desc'];
            $seo = $site;
        }
        $seo['title'] = str_replace('{city}',$this->cityInfo['name'],$seo['title']);
        $seo['keys'] = str_replace('{city}',$this->cityInfo['name'],$seo['keys']);
        $seo['desc'] = str_replace('{city}',$this->cityInfo['name'],$seo['desc']);
        $this->assign('seo',$seo);
    }
    private function checkUserLogin(){
        $info = cookie('userInfo');
        if($info && empty($info['headimgurl'])){
            $info['headimgurl'] = '/static/home/images/head_logo.png';
        }
        $this->userInfo = $info;
        $this->assign('userInfo',$info);
    }
    /**
     * 读取站点导航
     */
    private function getMenu(){
        $menu = cache('nav');
        if(!$menu){
            $map['status'] = 1;
            $lists = model('nav')->where($map)->order('ordid asc')->select();
            if($lists){
                $cate = objToArray($lists);//普通列表
                $temp = [];
                foreach($cate as $v){
                    if(!empty($v['alias'])){
                        $temp[$v['alias']] = $v;
                    }else{
                        $temp[$v['model']] = $v;
                    }
                }
                $menu = $temp;
                cache('nav',$temp);
            }

        }
            if(isset($menu[$this->controller]) && $this->menuSeo){
                $this->seo = [
                    'title'     =>$this->site['title'],
                    'seo_title' => empty($menu[$this->controller]['seo_title']) ? $menu[$this->controller]['title'] : $menu[$this->controller]['seo_title'],
                    'seo_keys'  => $menu[$this->controller]['seo_keys'],
                    'seo_desc'  => $menu[$this->controller]['seo_desc']
                ];
            }
        $this->assign('menu',$menu);
    }
    /**
     * @return mixed
     * 空操作 找不到操作方法时执行
     */
    public function _empty(){
        return $this->fetch('public/404');
    }
    private function getCate(){
        $cate = cache('oneCate');
        $articleCate = cache('articleCate');
        $this->assign('articleCate',$articleCate);
        $this->assign('oneCate',$cate);
    }
    //如果 不存在cookie 根据二级域名判断进入对应的城市
    private function getDomainByCity($second){
        if($second != config('url_domain_root') && $second != 'www'){
            $info =  db('city')->where(['domain'=>$second,'status'=>1])->find();
            if($info){
                $this->cityInfo = $info;
                cookie('cityInfo',$info);
            }else{
                $this->redirect('Index/index@www');
            }
        }else{
            $this->redirect('Index/index@www');
        }
    }
    private function getCity(){
        $city_cache = cache('city');
        $city = $city_cache['tree'];
        $hot_city = [];$temp = [];
        if($city){
            foreach($city as $v){
                if($v['is_hot'] == 1){
                    $hot_city[] = $v;
                }
                $first = strtoupper(substr($v['alias'],0,1));
                $temp[$first][] = $v;
            }
            ksort($temp);
        }
        $area = '';
        if($this->cityInfo){
            if(isset($city[$this->cityInfo['id']]['_child'])){
                $area = $city[$this->cityInfo['id']]['_child'];
            }else{
                $area = isset($city[$this->cityInfo['id']]) ? [$city[$this->cityInfo['id']]] : false;
            }
        }
        $this->assign('area',$area);
        $this->assign('hotCity',$hot_city);
        $this->assign('city',$temp);
        $this->assign('cityCate',$city_cache['cate']);
        $this->assign('cityTree',$city_cache['tree']);
    }
}